﻿using Newtonsoft.Json;
using NPinyin;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Net;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;

namespace mysqlDemo
{

    class Program
    {
        public static string charStr = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        public static string sqltemplate = "INSERT INTO student(name, age) values('{0}', {1});";
        public static TaskFactory taskFactory = new TaskFactory();
        public static List<string> temp = new List<string>();

        public static void AAA(ref int i, out int j)
        {
            j = 100;

        }
        static void Main(string[] args)
        {
            Console.WriteLine(@"Hello,\""\nworld");

            #region MyRegion

            //for (int j = 0; j < 10; j++)
            //{

            //    try
            //    {
            //        Task.Run(() =>
            //        {
            //            Console.WriteLine($"Task.Run value {j}");
            //            int value = 2 / (j % 2);
            //        });
            //    }
            //    catch
            //    {
            //        Console.WriteLine("Task.Run异常");
            //    }

            //    Thread.Sleep(10);
            //}

            //List<Task> taskList = new List<Task>();
            //for (int i = 0; i < 10; i++)
            //{
            //    taskList.Add(Task.Run(() =>
            //    {
            //        Console.WriteLine("当前线程:{0} 开始执行!", Thread.CurrentThread.ManagedThreadId);
            //        StringBuilder sb = new StringBuilder();
            //        Random r = new Random();

            //        for (int j = 0; j <= 100000; j++)
            //        {
            //            if (j != 0 && j % 2000 == 0)
            //            {
            //                Console.WriteLine("当前线程:{0} 开始执行 2000 条语句!", Thread.CurrentThread.ManagedThreadId);
            //                int result = MySqlHelper.ExecuteNonQuery(CommandType.Text, sb.ToString());
            //                sb.Clear();
            //            }
            //            sb.AppendFormat(sqltemplate, charStr.Substring(r.Next(1, 30), 8), new Random().Next(0, 100));
            //        }
            //        Console.WriteLine("当前线程:{0} 执行完成! ", Thread.CurrentThread.ManagedThreadId);
            //    }));
            //}

            //Task.Factory.ContinueWhenAll(taskList.ToArray(), t =>
            //{
            //    Console.WriteLine(" 一组计划执行完成:");
            //});
            //Console.ReadLine();
            #endregion

            List<Task> taskList = new List<Task>();

            for (var i = 0; i < 10; i++)
            {
                taskList.Add(GetTask(i));
            }
            Task.WaitAll(taskList.ToArray());

            Console.WriteLine(string.Join(",", temp.ToArray()));
        }
        static Task GetTask(int i)
        {
            return taskFactory.StartNew(() =>
            {
                temp.Add(i.ToString());
            });
        }
    }
}